package com.dingwei.lesyndrome.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.dingwei.lesyndrome.R;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.DisplayUtil;
import com.lihao.baselibrary.utils.ScreenUtils;
import com.luck.picture.lib.entity.LocalMedia;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created time 2018/3/2.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class CircleGridAdapter extends RecyclerArrayAdapter<LocalMedia> {

    public interface ChooseImgInterFace {
        void onPicClick(int position);

        void onRemoveClick(int position);
    }

    private ChooseImgInterFace chooseImgInterFaceListener;

    public void onChooseImgInterFaceListener(ChooseImgInterFace listener) {
        chooseImgInterFaceListener = listener;

    }

    public CircleGridAdapter(Context context) {
        super(context);
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new CircleGridHolder(parent);
    }

    public class CircleGridHolder extends BaseViewHolder<LocalMedia> {


        @Bind(R.id.img_pic)
        ImageView imgPic;
        @Bind(R.id.img_remove)
        ImageView imgRemove;

        public CircleGridHolder(ViewGroup parent) {
            super(parent, R.layout.item_circle_pic);
            ButterKnife.bind(this, itemView);
            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams((ScreenUtils.getScreenWidth(BaseActivity.context) - DisplayUtil.dip2px(BaseActivity.context, 62)) / 4, (ScreenUtils.getScreenWidth(BaseActivity.context) - DisplayUtil.dip2px(BaseActivity.context, 62)) / 4);
            imgPic.setLayoutParams(lp);
        }

        @Override
        public void setData(LocalMedia data) {
            super.setData(data);
            if (TextUtils.isEmpty(data.getCompressPath())) {
                imgPic.setImageResource(R.drawable.ic_upload_pic);
                imgRemove.setVisibility(View.GONE);
            } else {
                imgPic.setImageBitmap(BitmapFactory.decodeFile(data.getCompressPath()));
                imgRemove.setVisibility(View.VISIBLE);
            }

            imgPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (chooseImgInterFaceListener!=null){
                        chooseImgInterFaceListener.onPicClick(getDataPosition());
                    }
                }
            });
            imgRemove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (chooseImgInterFaceListener!=null){
                        chooseImgInterFaceListener.onRemoveClick(getDataPosition());
                    }
                }
            });

        }
    }
}
